import difference from 'lodash/difference';
/**
 *  转换ant table的sorter对象转换成分页的查询的排序字段
 * @param {object} sorter
 */
export const conventSorter = sorter => {
    const option = {};
    if(sorter)
        // 点击排序
        if(sorter.order && sorter.field) {
            option.sortField = sorter.field;
            if(sorter.order === 'descend')
                option.isDesc = true;
            else
                option.isDesc = false;
        } else { // 取消排序
            option.sortField = undefined;
            option.isDesc = undefined;
        }
    return option;
};

/**
 * 计算table的 selectedRows
 * @param {*} preKeys 上一个操作 返回的 selectedKeys
 * @param {*} preRows 上一个操作 返回的 selectedRows
 * @param {*} currentKeys 本次操作 返回的 selectedKeys
 * @param {*} currentRows 本次操作 返回的 selectedRows
 * @param {*} keyIndex eg: id
 */
export const computeSelectedRows = (preKeys, preRows, currentKeys, currentRows, keyIndex = 'id') => {
    if(currentKeys.length === 0)
        return [];
    // 勾选
    if(currentKeys.length > preKeys.length) {
        const newKeys = difference(currentKeys, preKeys);
        const newRows = [];
        newKeys.forEach(key => {
            const row = currentRows.find(r => r[keyIndex] === key);
            if(row)
                newRows.push(row);
        });
        return [...preRows, ...newRows];
    } else if(currentKeys.length < preKeys.length) { // 取消勾选
        const deleteKeys = difference(preKeys, currentKeys);
        const newRows = preRows.filter(d => !deleteKeys.includes(d[keyIndex]));
        return newRows;
    }
    return preRows;
};

export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);

export const sortPartAndCategory = (a, b) => {
    if(a.usedPartCode === b.usedPartCode)
        if(a.productCategoryCode === b.productCategoryCode) {
            if(a.usedPartCode > b.usedPartCode)
                return 1;
            return -1;
        } else if(a.productCategoryCode > b.productCategoryCode)
            return 1;
        else
            return -1;
    else if(a.usedPartCode > b.usedPartCode)
        return 1;
    else
        return -1;
};

export const isfileImg = fileName => {
    if(!fileName || typeof fileName != 'string')
        return false;
    const fileNameReverse = fileName.split('').reverse().join('');
    const name = fileNameReverse.substring(0, fileNameReverse.search(/\./)).split('').reverse()
        .join('');
    switch(name) {
        case 'png':
            return true;
        case 'jpg':
            return true;
        case 'webp':
            return true;
        case 'svg':
            return true;
        case 'gif':
            return true;
        case 'jpeg':
            return true;
        case 'bmp':
            return true;
        case 'dpg':
            return true;
        default:
            return false;
    }
};

import qs from 'qs';
export const getUrl = str => {
    const r = qs.parse(str.replace('?', ''));
    if(r !== null)
        return r;
    return null;
};
